<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Reg extends CI_Controller {
	
	function index(){
		$ret['code'] = 0;
		$data = array(
			'username'      => trim($this->input->get_post('username')),
			'email'         => trim($this->input->get_post('email')),
			'password'		=> md5(md5($this->input->get_post('password'))),
			'invite_userid' => intval($this->input->get_post('invite')),
			'email_status'  => '0',
			'type_id'       => '2',
			'add_time'      => time(),
			'addip'         => $this->input->ip_address(),
		);
		
		if ($data['username']==""){
			$ret['msg'] = '用户名不能为空';
			$ret['code'] = 1;
		}elseif(!isset($data['username'][2])){
			$ret['msg'] = '用户名太短';
			$ret['code'] = 2;
		}elseif(!$this->myformcheck->isName($data['username'])){
			$ret['msg'] = '非法的用户名';
			$ret['code'] = 3;
		}elseif($data['email']==""){
			$ret['msg'] = '邮箱不能为空';
			$ret['code'] = 4;
		}elseif(!$this->myformcheck->isEmail($data['email'])){
			$ret['msg'] = '无效的邮箱';
			$ret['code'] = 5;
		}elseif ($this->input->get_post('password')==""){
			$ret['msg'] = '密码不能为空';
			$ret['code'] = 6;
		}elseif($this->input->get_post('password') != $this->input->get_post('password2')){
			$ret['msg'] = '两次密码不一致';
			$ret['code'] = 7;
		}else{
			$this->load->model('User_model', 'user');
			if($this->user->get_one(array('username'=>$data['username']), 'user_id')){
				$ret['msg'] = '用户名已存在';
				$ret['code'] = 8;
			}elseif($this->user->get_one(array('email'=>$data['email']), 'user_id')){
				$ret['msg'] = '邮箱已存在';
				$ret['code'] = 9;
			}else{
				/** UC同步注册 */
				/*include './config.inc.php';
				include './uc_client/client.php';
				$uid = uc_user_register(iconv('UTF-8', 'GBK', $data['username']), $this->input->get_post('password'), $data['email']);
				if($uid <= 0) {
					if($uid == -1) {
						$ret['msg'] = '非法的用户名';
						$ret['code'] = 3;
					} elseif($uid == -2) {
						$ret['msg'] = '非法的用户名';
						$ret['code'] = 3;
					} elseif($uid == -3) {
						$ret['msg'] = '用户名已存在';
						$ret['code'] = 8;
					} elseif($uid == -4) {
						$ret['msg'] = '无效的邮箱';
						$ret['code'] = 5;
					} elseif($uid == -5) {
						$ret['msg'] = '该邮箱不允许注册';
						$ret['code'] = 10;
					} elseif($uid == -6) {
						$ret['msg'] = '邮箱已存在';
						$ret['code'] = 9;
					} else {
						$ret['msg'] = '论坛注册失败，请与客服联系';
						$ret['code'] = 11;
					}
				}else{*/
					$user_id = $this->user->add($data);
					if($user_id){
						$msg = array(
							'user_id'=> $user_id,
							'username' => $data['username'],
							'webname' => CON_PLATFORMNAME,
							'email' => $data['email']
						);
						//发送注册邮件
						$this->load->library('func');
						$this->load->library('/phpmailer/PHPMailer');
						$msgBody = $this->func->regEmailMsg($msg);
						$this->phpmailer->IsSMTP();
						$this->phpmailer->AddAddress($data['email'], $data['username']);//收件人
						$this->phpmailer->Subject 	= CON_PLATFORMNAME."注册认证";
						$this->phpmailer->Body 		= $msgBody;
						$res = $this->phpmailer->Send();
						
						//注册成功自动登陆
						$user = array(
							'user_id' => $user_id,
							'username' => $data['username'],
							'email' => $data['email'],
							'login_time' => time()
						);
						//$this->session->set_userdata('ych_user',$user);
						
						$ret['msg'] = '注册成功，请尽快前往邮箱激活';
						$ret['code'] = 0;
					}else{
						$ret['msg'] = '注册失败，请联系客服';
						$ret['code'] = 11;
					}
				//}
			}
		}
		echo json_encode($ret);
	}
	
}